﻿using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using WroxGallery.Controllers;
using WroxGallery.Models;
using WroxGallery.Models.Interfaces;

namespace WroxGallery.Tests.Controllers
{
    /// <summary>
    /// Summary description for AdminControllerTest
    /// </summary>
    [TestClass]
    public class AdminControllerTest
    {
        private MockRepository mocks;
        private IConfigurationRepository configurationRepository;

        [TestInitialize]
        public void SetupController()
        {
            mocks = new MockRepository();
            configurationRepository = mocks.DynamicMock<IConfigurationRepository>();
        }

        [TestMethod]
        public void AdminController_Index_Action_Retreive_Configuration_Data()
        {
            GalleryConfiguration config = new GalleryConfiguration();
            config.GalleryName = "Gallery Name";
            using (mocks.Record())
            {
                Expect.Call(configurationRepository.Config).Return(config);
            }

            AdminController controller = new AdminController(configurationRepository);
            ViewResult result = controller.Index() as ViewResult;

            Assert.IsNotNull(result, "Not a view result");

            GalleryConfiguration returnedConfig = result.ViewData.Model as GalleryConfiguration;

            Assert.IsNotNull(returnedConfig, "Not a Gallery Configuration");
            Assert.AreEqual("Gallery Name", returnedConfig.GalleryName);
        }


        [TestMethod]
        public void AdminController_Index_Action_Store_Configuration_Data()
        {
            GalleryConfiguration config = new GalleryConfiguration();
            using (mocks.Record())
            {
                Expect.Call(delegate { configurationRepository.Store(); });
            }

            AdminController controller = new AdminController(configurationRepository);

            using (mocks.Playback())
            {
                RedirectToRouteResult result = controller.Index(config) as RedirectToRouteResult;

                Assert.IsNotNull(result, "Not a redirect result");

                Assert.AreEqual("Index", result.RouteValues["Action"]);
            }
        }
    }
}
